package com.dhecp.project.system.controller;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.project.system.domain.SysVersion;
import com.dhecp.project.system.service.SysVersionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName SysVersionController
 * @Description TODO
 * @Author alula
 * @Date 2020-03-12 11:25
 */
@Controller
@RequestMapping("/system/version")
public class SysVersionController extends BaseController {

    private String prefix = "system/version";

    @Autowired
    private SysVersionService sysVersionService;

    @RequiresPermissions("system:version:view")
    @GetMapping()
    public String version()
    {
        return prefix + "/version";
    }

    @RequiresPermissions("system:version:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params)
    {
        startPage();
        List<SysVersion> list = sysVersionService.queryVersionList(params);
        return getDataTable(list);
    }

    @RequiresPermissions("system:version:view")
    @RequestMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("versioin", sysVersionService.queryVersionById(id));
        return prefix + "/version_detail";
    }

    @RequiresPermissions("system:version:add")
    @RequestMapping("/add")
    public String add(ModelMap mmap) {
        SysVersion versioin = new SysVersion();
        versioin.setDeleted(false);
        mmap.put("versioin", versioin);
        return prefix + "/version_add";
    }

    @Log(title = "软件版本新增", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:version:add")
    @PostMapping("/addpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(SysVersion sysVersion)
    {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= sysVersionService.insertVersion(sysVersion);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }


    @RequiresPermissions("system:version:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        SysVersion versioin = sysVersionService.queryVersionById(id);
        mmap.put("versioin", versioin);
        return prefix + "/version_add";
    }

    @Log(title = "软件版本修改", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:version:edit")
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editSave(SysVersion sysVersion)
    {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= sysVersionService.updateVersion(sysVersion);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    @RequiresPermissions("system:version:delete")
    @Log(title = "软件版本删除", businessType = BusinessType.DELETE)
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult deleteVersion(@RequestParam("id") String id) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = sysVersionService.deleteVersion(id);
            return ajaxResult;
        }
        catch (Exception e) {
            return AjaxResult.error(this.getExceptionMessage(e));
        }
    }
}
